/*--------------------------------------------	
Database For Shop Weapons
--------------------------------------------*/
ShopWeapons = {}
function AddShopWeapon(tier, name, entity, model, price, classes)

	if not name or not entity or not price then 
		hook.Add("PlayerSpawn", "ItemError", function(ply)
		if ply:IsAdmin() then ply:ChatPrint("WARNING: Weapon Item made incorrectly, failed to load!") end end) 
		return
	end
	
	if type(classes) == "number" then
		classes = {classes}
	end
	table.insert(ShopWeapons, {slot = tier, name = name, ent = entity, model = model, price = price, allowed = classes })
end

/*--------------------------------------------	
Adds Weapons to the shop
--------------------------------------------*/
--rifles
AddShopWeapon(1, "M4", "weapon_mad_m4", "models/weapons/w_rif_m4a1.mdl", 300, { "Fed_Gunner","Raider_Gunner" })
AddShopWeapon(1, "AK47", "weapon_mad_ak47", "models/weapons/w_rif_ak47.mdl", 500, { "Fed_Gunner","Raider_Gunner" })

--shotguns
AddShopWeapon(1, "M3", "weapon_mad_m3", "models/weapons/w_shot_m3super90.mdl", 300, { "Fed_Shotgunner","Raider_Shotgunner" })

--pistols
AddShopWeapon(2, "p228", "weapon_mad_p228", "models/weapons/w_pist_p228.mdl", 200, { "Fed_Gunner","Raider_Gunner","Fed_Shotgunner","Raider_Shotgunner" })
AddShopWeapon(2, "Glock", "weapon_mad_glock", "models/weapons/w_pist_glock18.mdl", 250, { "Fed_Gunner","Raider_Gunner","Fed_Shotgunner","Raider_Shotgunner" })



/*--------------------------------------------	
Database For Shop AMmo
--------------------------------------------*/
ShopAmmo = {}
function AddShopAmmo( name, entity, model, price, classes)

	if not name or not entity or not price then 
		hook.Add("PlayerSpawn", "ItemError", function(ply)
		if ply:IsAdmin() then ply:ChatPrint("WARNING: Ammo item made incorrectly, failed to load!") end end) 
		return
	end
	
	if type(classes) == "number" then
		classes = {classes}
	end
	table.insert(ShopAmmo, {name = name, typ = entity, model = model, price = price, allowed = classes })
end

/*--------------------------------------------	
Adds Ammo to the shop
--------------------------------------------*/
AddShopAmmo("Rifle Ammo", "AR2", "models/weapons/rifleshell.mdl", 0.3, { "Fed_Gunner","Raider_Gunner" })
AddShopAmmo("Shotgun Ammo", "Buckshot", "models/weapons/shotgun_shell.mdl", 0.4, { "Fed_Shotgunner","Raider_Shotgunner" })
AddShopAmmo("Handgun Ammo", "Pistol", "models/weapons/shell.mdl", 0.2, { "Fed_Gunner","Raider_Gunner","Fed_Shotgunner","Raider_Shotgunner"  })

/*--------------------------------------------	
Cross Class TableGivan
--------------------------------------------*/
function GM:GiveTable(tbl)
	return tbl
end

/*--------------------------------------------	
Gieb Weapon Plz
--------------------------------------------*/
function CheckAllowed(classply, classtable)
	for _,v in pairs(classtable) do
		if v == classply then
			return 1
		end
	end
end

function PurchaseWeapon( pl, handler, id, encoded, decoded )
	local SentTable = decoded.table
	local PlayerClass = decoded.string

	local WeaponClass = tostring(pl:GetWeapons()[SentTable['slot']]:GetClass())
	local BuyName = SentTable['name']
	local BuyClass = SentTable['ent']
	local BuyPrice = SentTable['price']
	local ClassAllow = SentTable['allowed']
	local CanBuy = (CheckAllowed(PlayerClass,ClassAllow) || 0)

	if(CanBuy == 1)then
		if(pl:GetMoney() > BuyPrice)then
			pl:StripWeapon(WeaponClass)
			pl:Give(BuyClass)
			pl:TakeMoney(BuyPrice)

			GAMEMODE:Push(pl, "Bought "..BuyName.." for $"..tostring(BuyPrice), 3)
			
		elseif(pl:GetMoney() < BuyPrice)then
			GAMEMODE:Push(pl, "You are broke!", 3)
		end
		
	elseif(CanBuy == 0)then
		GAMEMODE:Push(pl, "Your class cannot buy this", 3)
	end
	
end
datastream.Hook( "PurchaseWeapon", PurchaseWeapon );

/*--------------------------------------------	
Gieb ammo Plz
--------------------------------------------*/
function PurchaseAmmo( pl, handler, id, encoded, decoded )

	local BoughtTable = decoded.bought
	local AmmoClass = tostring(BoughtTable.typ)
	
	local AmmoTable = decoded.current
	local MaxAmmoTable = decoded.max
	
	local Ammo_MAX = MaxAmmoTable[AmmoClass] 
	local Ammo_ON = pl:GetAmmoCount(AmmoClass)
	
	local Markup = BoughtTable.price
	local FillPrice = math.floor((Ammo_MAX - Ammo_ON) * Markup)
	local PlyCash = math.floor(pl:GetMoney() / Markup)
	
	local ClassAllow = BoughtTable['allowed']
	local PlayerClass = decoded.class
	local CanBuy = (CheckAllowed(PlayerClass,ClassAllow) || 0)
	
	if(CanBuy)then
		if(Ammo_ON < Ammo_MAX)then			
				if(PlyCash >= FillPrice)then
					pl:TakeMoney(FillPrice)
					pl:GiveAmmo((Ammo_MAX - Ammo_ON) ,AmmoClass )
					GAMEMODE:Push(pl, "Filled your ammo supply", 5)
					
				elseif(PlyCash < FillPrice and PlyCash > 5)then
					pl:TakeMoney(math.floor(PlyCash * Markup))
					pl:GiveAmmo(math.floor(PlyCash ) ,AmmoClass )
					GAMEMODE:Push(pl, "Bought ".. (math.floor(PlyCash)) .." units", 5)					
					
				elseif(PlyCash < 5)then
					GAMEMODE:Push(pl, "You're broke", 1)
					
				end				
				--safty check
				if(pl:GetAmmoCount(AmmoClass) > Ammo_MAX)then
					pl:GiveAmmo(Ammo_ON - Ammo_MAX ,AmmoClass )
				end
				
		elseif(Ammo_ON >= Ammo_MAX)then
			print("Player has enough ammo")
		end
	elseif(!CanBuy)then
		GAMEMODE:Push(pl, "Ammo not for your class", 1)
	end

end

datastream.Hook( "PurchaseAmmo", PurchaseAmmo );